using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using AppManagement.Models;
	using AppManagement.Repositories;
	using AppManagement.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	#endregion

	public partial class FrmQuanLyKeHoachDaoTao : Office2007Form
	{
		public EventHandler Handler { get; set; }
        private int MaCN;
		public FrmQuanLyKeHoachDaoTao(int macn)
		{
			InitializeComponent();
            MaCN = macn;
		}

		private void FrmQuanLyKeHoachDaoTao_Load(object sender, EventArgs e)
		{
			InitGridView();
			LoadData();
			dtgvData.ClearSelection();
		}

		private void btnThem_Click(object sender, EventArgs e)
		{
            FrmLapKeHoachDaoTao frm = new FrmLapKeHoachDaoTao(this.MaCN);
			frm.Handler += LoadData;
			frm.ShowDialog();
		}

		private void btnXoa_Click(object sender, EventArgs e)
		{
			if (dtgvData.SelectedRows.Count > 0)
			{
				if (MessageBoxEx.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					if (KeHoachDaoTaoRepository.Delete(int.Parse(dtgvData.SelectedRows[0].Cells["MaKHDT"].Value.ToString())))
					{
						MessageBoxEx.Show("Xóa dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
						LoadData();
					}
					else
					{
						MessageBoxEx.Show("Xóa dữ liệu thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}

		private void btnCapNhat_Click(object sender, EventArgs e)
		{
			if (dtgvData.SelectedRows.Count > 0)
			{
				FrmLapKeHoachDaoTao frm = new FrmLapKeHoachDaoTao(int.Parse(dtgvData.SelectedRows[0].Cells["MaKHDT"].ToString()));
				frm.Handler += LoadData;
				frm.ShowDialog();
			}
		}

		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Mã KHDT",
				DataPropertyName = "MaKHDT",
				Name = "MaKHDT",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5)) - 50
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Chuyên ngành",
				DataPropertyName = "TenCN",
                Name = "TenCN",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5)) + 10
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "TenGoi",
				DataPropertyName = "TenGoi",
				Name = "TenGoi",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5)) + 100
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "NamHoc",
				DataPropertyName = "NamHoc",
				Name = "NamHoc",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5)) - 30
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "HocKy",
				DataPropertyName = "HocKy",
				Name = "HocKy",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5)) - 30
			};
			dtgvData.Columns.Add(objColumn);
		}

		private void LoadData(object sender, EventArgs e)
		{
			LoadData();
		}

		private void LoadData()
		{
			var lstItem = KeHoachDaoTaoRepository.SelectByMaCN(this.MaCN);
			var dt = new DataTable();
			dt.Columns.Add("MaKHDT");
			dt.Columns.Add("TenCN");
			dt.Columns.Add("TenGoi");
			dt.Columns.Add("NamHoc");
			dt.Columns.Add("HocKy");
			for (int i = 0; i < lstItem.Count; i++)
			{
				dt.Rows.Add(lstItem[i].MaKHDT, lstItem[i].ChuyenNganh.TenCN, lstItem[i].TenGoi, lstItem[i].NamHoc, lstItem[i].HocKy);
			}
			DataView objTimeSheetDV = new System.Data.DataView(dt);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}

		private void dtgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
		{
			if (dtgvData.SelectedRows.Count > 0)
			{
				FrmLapKeHoachDaoTao frm = new FrmLapKeHoachDaoTao(this.MaCN, int.Parse(dtgvData.SelectedRows[0].Cells["MaKHDT"].Value.ToString()));
				frm.Handler += LoadData;
				frm.ShowDialog();
			}
		}

	}

}
